package rabbitMQStudy;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Project: tobe-better-coder
 * Package: rabbitMQStudy
 *
 * @author : Mr.xu
 * @date : 2023/5/6 7:27
 */
public class Consumer {

    public static final String QUEUE_NAME = "hello";

    public static void main(String[] args) throws IOException, TimeoutException {

        Channel channel = rabbitMQStudy.CommonUtil.getChannel();

        DeliverCallback deliverCallback = (consumerTag, message) -> {
            System.out.println(new String(message.getBody()));
            channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
        };

        CancelCallback cancelCallback = consumerTag -> {
            System.out.println("消费消息中断");
        };

        /**
         * String queue 队列名称
         * boolean autoAck 是否自动应答，如果为false的话，就要手动应答自己消费了消息，否则消息就会在Unacked，表示为消费失败
         *DeliverCallback deliverCallback 消费成功回调
         * CancelCallback cancelCallback 消费失败回调
         */
        System.out.println("c2等待消费消息......");
        channel.basicConsume(QUEUE_NAME, false, deliverCallback, cancelCallback);
    }
}